package com.kandinfo.survey.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.kandinfo.survey.common.domain.QuestionnaireDetail;
import com.kandinfo.survey.common.domain.QuestionnaireInfo;
import com.kandinfo.survey.common.dto.QuestionnaireResposeDto;
import com.kandinfo.survey.common.mongo.QcQuestionDto;
import com.kandinfo.survey.common.service.MongoQuestionService;
import com.kandinfo.survey.common.service.QuestionnaireSerivce;
import com.kandinfo.survey.questionnaire.service.QuestionnaireDetailService;
import com.kandinfo.survey.questionnaire.service.QuestionnaireInfoService;

@Service
public class QuestionnaireSerivceImpl implements QuestionnaireSerivce {

    private static final Logger logger = LoggerFactory.getLogger(QuestionnaireSerivceImpl.class);

    @Autowired
    QuestionnaireInfoService infoService;
    @Autowired
    QuestionnaireDetailService detailService;
    @Autowired
    MongoQuestionService mongoQuestionService;

    @Override
    public List<QuestionnaireResposeDto> get(Long questionnaireId, List<Long> idList) {
        if (questionnaireId == null || CollectionUtils.isEmpty(idList)) {
            return null;
        }

        List<QuestionnaireResposeDto> result = new ArrayList<QuestionnaireResposeDto>();
        if (questionnaireId != null && CollectionUtils.isEmpty(idList)) {
            // case 1 : 查单个
            result.add(getOne(questionnaireId));
        } else {
            // case 2 : 查多个
            idList.forEach(qId -> {
                result.add(getOne(questionnaireId));
            });
        }
        return result;
    }

    /**
     * 查询单个
     * 
     * @param questionaireId
     * @return
     */
    private QuestionnaireResposeDto getOne(Long questionnaireId) {
        // return object
        QuestionnaireResposeDto responseDto = new QuestionnaireResposeDto();
        // base data
        QuestionnaireInfo infoDomain = infoService.findOne(questionnaireId);
        if (infoDomain == null) {
            logger.error("[NULL] QuestionnaireInfo : {}" + questionnaireId);
            return null;
        }
        List<QuestionnaireDetail> detailDomains = detailService.findOneByQuesnaireId(questionnaireId);
        if (CollectionUtils.isEmpty(detailDomains)) {
            logger.error("[NULL] QuestionnaireDetails : {}" + questionnaireId);
            return null;
        }

        List<Long> qIds = new ArrayList<>();
        detailDomains.forEach(detailDomain -> {
            qIds.add(detailDomain.getQuestionId());
        });

        List<QcQuestionDto> mongoQuestions = mongoQuestionService.findByCriteria(new Criteria("_id").in(qIds));
        if (CollectionUtils.isEmpty(mongoQuestions)) {
            logger.error("[NULL] QcQuestionDto : {}" + qIds.toString());
            return null;
        }

        // set data
        responseDto.setQuestions(mongoQuestions);
        responseDto.setStatus(infoDomain.getStatus());
        responseDto.setDesc(infoDomain.getDesc());
        responseDto.setDestType(infoDomain.getDestType());
        responseDto.setName(infoDomain.getName());
        return responseDto;
    }

}
